package com.controller;

import com.entry.Productcollect;
import com.entry.Result;
import com.server.ProductcollectService;
import com.util.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProductcollectController {

    @Autowired
    RedisUtil redisUtil;
    @Resource
    ProductcollectService productcollectService;

    @RequestMapping(value = "/collect/add",method = RequestMethod.GET)
    @ResponseBody
    public Result addProductCollect(@Param("productcollect_product_id") int productcollect_product_id){
        Result result = new Result();
        String member_id=String.valueOf(redisUtil.get("member_id"));
        int productcollect_member_id = Integer.parseInt(member_id);
        //判断缓存是否有用户的member_id
        if (member_id!=null){
            int count=productcollectService.getIsCollect(productcollect_product_id, productcollect_member_id);
            if (count>0){
                result.setSuccess(true);
                return result;
            }
            int count1=productcollectService.addProductCollect(productcollect_product_id, productcollect_member_id);
            if (count1>0){
                result.setSuccess(true);
                return result;
            }
        }
        result.setSuccess(false);
        return result;
    }

    @RequestMapping(value = "/collect/all",method = RequestMethod.GET)
    @ResponseBody
    public Result getAllProductCollect(HttpSession session){
        Result result = new Result();
        String member_id=String.valueOf(redisUtil.get("member_id"));
        int productcollect_member_id = Integer.parseInt(member_id);
        //判断缓存是否有用户的member_id
        if (member_id!=null){
            List<Productcollect> productcollectList =productcollectService.getAllProductCollect(productcollect_member_id);
            session.setAttribute("productcollectList",productcollectList);
            productcollectList.forEach(System.out::println);
            System.out.println(productcollectList.size());
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    @RequestMapping(value = "/collect/delete",method = RequestMethod.GET)
    @ResponseBody
    public Result RemoveProductCollect(int productcollect_product_id){
        Result result = new Result();
        String member_id=String.valueOf(redisUtil.get("member_id"));
        int productcollect_member_id = Integer.parseInt(member_id);
        //判断缓存是否有用户的member_id
        if (member_id!=null){
            int count=productcollectService.RemoveProductCollect(productcollect_product_id, productcollect_member_id);
            if (count>0){
                result.setSuccess(true);
                return result;
            }

        }
        result.setSuccess(false);
        return result;
    }

}
